package cn.kailangtu.serialutils.api;

import android.os.SystemClock;
import android.util.Log;

import java.util.concurrent.LinkedBlockingQueue;

import cn.kailangtu.commontools.APPSPUtils;
import cn.kailangtu.commontools.AppVariable;
import cn.kailangtu.commontools.CommonUtils;
import cn.kailangtu.commontools.socket.TemperatureManager;

public class SerialPort485Manager {


    ReceiveMsgListenerV2 comHongwaiListener;

    private SerialPortApi485 comPlc;
    private SerialPortApi485 comDianNeng;
    private SerialPortApi485 comHongWai;


   // private SerialPortApi485Temperature com2;
    String TAG = "SerialPort485Manager";
    private static SerialPort485Manager instance;

    private SerialPort485Manager(){

    }

    public static SerialPort485Manager getInstance(){
        if(instance == null){
            instance = new SerialPort485Manager();
        }
        return instance;
    }


    public void initSerialPort() {
        Log.d(TAG, "初始化");
        comPlc = new SerialPortApi485(Com1.getInstance());
        comDianNeng = new SerialPortApi485(Com2.getInstance());
        comHongWai = new SerialPortApi485(Com3.getInstance());
        comPlc.openSerialPort(APPSPUtils.getInstance().getSerialPort1(),
                Integer.parseInt(APPSPUtils.getInstance().getSerialBaudPort1()),
                APPSPUtils.getInstance().getSerialCheckPort1()
        );


        comDianNeng.openSerialPort(APPSPUtils.getInstance().getSerialPort2(), Integer.parseInt(APPSPUtils.getInstance().getSerialBaudPort2()),
                APPSPUtils.getInstance().getSerialCheckPort2()
        );

        comHongWai.setListener(new ReceiveMsgListenerV2() {
            @Override
            public void onReceiveData(byte[] data) {
                if (comHongwaiListener != null) {
                    comHongwaiListener.onReceiveData(data);
                }

            }
        });


        comHongWai.openSerialPort("/dev/ttyHSL2", 9600, 0);


        //com1.openSerialPort("/dev/ttyHSL1", 38400, 0);
        //com2.openSerialPort("/dev/ttyHSL2", 115200, 0);
        TemperatureManager.getInstance();
    }



    public byte[] sendString(int comNum,byte[] data)
    {
        //Log.d(TAG,"send String:"+comNum+" |||"+data);

        if(comNum == 1){
            if(comPlc != null) {
                Log.d("COMCOM1","发数据了");
                comPlc.sendAndReceive(data);
            }
        }
        else if(comNum == 2)
        {
            if(comDianNeng != null) {
                Log.d("COMCOM2","发数据了");
                comDianNeng.sendAndReceive(data);
            }


        }
        return null;
    }

    public void setComHongwaiListener(ReceiveMsgListenerV2 comHongwaiListener) {
        this.comHongwaiListener = comHongwaiListener;
    }

}
